package com.cleaningcloud.cld.service.impl;

import com.cleaningcloud.cld.domain.CldContract;
import com.cleaningcloud.cld.domain.dto.CldWorkplanDto;
import com.cleaningcloud.cld.domain.dto.CldWorkplanSubmitDto;
import com.cleaningcloud.cld.domain.vo.CldWorkplanVo;
import com.cleaningcloud.cld.mapper.CldContractMapper;
import com.cleaningcloud.cld.mapper.CldWorkplanDetailsMapper;
import com.cleaningcloud.cld.mapper.CldWorkplanMapper;
import com.cleaningcloud.cld.mapper.CldWorkplanSubmitMapper;
import com.cleaningcloud.cld.service.ICldWorkplanService;
import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 工作计划Service业务层处理
 *
 * @author cleaningcloud
 * @date 2024-10-17
 */
@Service
public class CldWorkplanServiceImpl implements ICldWorkplanService {
    @Autowired
    private CldWorkplanMapper cldWorkplanMapper;

    @Autowired
    private CldWorkplanDetailsMapper workplanDetailsMapper;

    @Autowired
    private CldContractMapper cldContractMapper;

    @Autowired
    private CldWorkplanSubmitMapper cldWorkplanSubmitMapper;

    /**
     * 查询工作计划
     *
     * @param workplanId 工作计划主键
     * @return 工作计划
     */
    @Override
    public CldWorkplanVo selectCldWorkplanByWorkplanId(Long workplanId) {
        return cldWorkplanMapper.selectCldWorkplanByWorkplanId(workplanId);
    }

    /**
     * 查询工作计划列表
     *
     * @param cldWorkplan 工作计划
     * @return 工作计划
     */
    @Override
    public List<CldWorkplanVo> selectCldWorkplanList(CldWorkplanDto cldWorkplan) {
        return cldWorkplanMapper.selectCldWorkplanList(cldWorkplan);
    }

    @Override
    public List<Map<String, Object>> selectCldWorkplanGroupProjectList(CldWorkplanDto cldWorkplanDto) {
        return cldWorkplanMapper.selectCldWorkplanGroupProjectList(cldWorkplanDto);
    }

    /**
     * 新增工作计划
     *
     * @param cldWorkplan 工作计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCldWorkplan(CldWorkplanDto cldWorkplan) {
        // 查询该项目当前岗位是否已添加工作计划
        int count = cldWorkplanMapper.selectCountByProjectIdAndPostId(cldWorkplan.getProjectId(), cldWorkplan.getPostId(), cldWorkplan.getCleanNature());
        if (count > 0) {
            throw new RuntimeException("该项目当前岗位已添加[" + (cldWorkplan.getCleanNature() == 1 ? "日清洁" : "专项清洁") + "]工作计划，请勿重复添加！");
        }
        cldWorkplan.setCreateTime(DateUtils.getNowDate());
        cldWorkplan.setCreateId(SecurityUtils.getUserId());
        cldWorkplan.setCreateName(SecurityUtils.getUsername());
        cldWorkplan.setFlag(0L);
        // 设置工作计划开始结束时间 为合同开始日期-结束日期
        CldContract cldContract = cldContractMapper.selectCldContractByProjectId(cldWorkplan.getProjectId());
        if (cldContract == null) {
            throw new RuntimeException("项目合同信息查询失败！");
        }
        cldWorkplan.setWorkStartDate(cldContract.getStartDate());
        cldWorkplan.setWorkEndDate(cldContract.getEndDate());
        cldWorkplanMapper.insertCldWorkplan(cldWorkplan);
        // 设置工作计划编
        // 批量新增工作计划明细
        if (cldWorkplan.getDetailsList() != null && !cldWorkplan.getDetailsList().isEmpty()) {
            cldWorkplan.getDetailsList().forEach(item -> {
                item.setWorkplanId(cldWorkplan.getWorkplanId());
            });
            workplanDetailsMapper.batchInsertWorkplanDetailsDetails(cldWorkplan.getDetailsList());
        }
        return 1;
    }

    /**
     * 修改工作计划
     *
     * @param cldWorkplan 工作计划
     * @return 结果
     */
    @Override
    @Transactional
    public int updateCldWorkplan(CldWorkplanDto cldWorkplan) {
        // 批量新增工作计划明细
        if (cldWorkplan.getDetailsList() != null && !cldWorkplan.getDetailsList().isEmpty()) {
            cldWorkplan.getDetailsList().forEach(item -> {
                if (item.getWorkplanDetailsId() == null) {
                    item.setWorkplanId(cldWorkplan.getWorkplanId());
                    workplanDetailsMapper.insertCldWorkplanDetails(item);
                } else {
                    workplanDetailsMapper.updateCldWorkplanDetails(item);
                }
            });
        }
        return cldWorkplanMapper.updateCldWorkplan(cldWorkplan);
    }

    @Override
    public int auditCldWorkplanSubmit(CldWorkplanSubmitDto cldWorkplanSubmitDto) {
        return cldWorkplanSubmitMapper.updateCldWorkplanSubmit(cldWorkplanSubmitDto);
    }

    /**
     * 批量删除工作计划
     *
     * @param workplanIds 需要删除的工作计划主键
     * @return 结果
     */
    @Override
    public int deleteCldWorkplanByWorkplanIds(Long[] workplanIds) {
        return cldWorkplanMapper.deleteCldWorkplanByWorkplanIds(workplanIds);
    }

    /**
     * 删除工作计划信息
     *
     * @param workplanId 工作计划主键
     * @return 结果
     */
    @Override
    public int deleteCldWorkplanByWorkplanId(Long workplanId) {
        return cldWorkplanMapper.deleteCldWorkplanByWorkplanId(workplanId);
    }
}
